<template>
  <el-dialog title="血糖监测" v-model="visible"
             :close-on-click-modal="false" draggable>

    <!--    <customer ref="customerInfoRef" :customerInfo="customerInfo"/>-->
    <customer-page ref="customerInfoRef" :customerInfo="customerInfo"/>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item label="是否空腹" prop="isFasting">
            <el-radio-group v-model="form.isFasting">
              <el-radio :label="item.value" v-for="(item, index) in bloodx_fasting" border :key="index">{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="血糖值" prop="number">
            <el-input v-model="form.number" placeholder="请输入血糖值"/>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item label="检测时间" prop="detectionTime">
            <el-date-picker type="datetime" placeholder="请选择检测时间" v-model="form.detectionTime"
                            :value-format="dateTimeStr"></el-date-picker>
          </el-form-item>
        </el-col>


      </el-row>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="BloodxCustomerBloodDialog">
import {useDict} from '/@/hooks/dict';
import {useMessage} from "/@/hooks/message";
import {addObj, putObj} from '/@/api/blood/bloodxCustomerBlood'

const CustomerPage = defineAsyncComponent(() => import('../../../components/Customer/index.vue'));


// import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
const customerInfoRef = ref()
// 定义字典
const {bloodx_fasting} = useDict('bloodx_fasting')

// 提交表单数据
const form = reactive({
  customerId: '',
  id: '',
  isFasting: '',
  number: '',
  detectionTime: '',
});

const customerInfo = ref({});


// 定义校验规则
const dataRules = ref({
  isFasting: [{required: true, message: '请选择是否空腹', trigger: 'change'}],
  number: [{required: true, message: '请输入血糖值', trigger: 'blur'}],
  detectionTime: [{required: true, message: '请选择检测时间', trigger: 'change'}],
})

// 打开弹窗
const openDialog = (row: any) => {
  visible.value = true
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });
  if (row) {
    form.customerId = row.id
    customerInfo.value = row
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => {
  });
  if (!valid) return false;

  try {
    loading.value = true;
    await addObj(form);
    useMessage().success('添加成功');
    visible.value = false;
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
// const getbloodxCustomerBloodData = (id: string) => {
//   // 获取数据
//   loading.value = true
//   getObj(id).then((res: any) => {
//     Object.assign(form, res.data)
//   }).finally(() => {
//     loading.value = false
//   })
// };

// 暴露变量
defineExpose({
  openDialog
});
</script>
